package demo4.mapper;

import demo4.Util.IDBUtil;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Component
public class Usermapper implements IUserMapper {
    @Autowired
    private IDBUtil dbUtil;

    @Override
    public int insert(User user) {
        System.out.println("Usermapper :像表中插入一条数据");
        try (Connection connection = dbUtil.getConnection();) {
            String sql = "INSERT INTO user(username,password,nickname,age) " +
                    "VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getNickname());
            ps.setInt(4, user.getAge());
            return ps.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }

    public User query(String username,String password) throws SQLException {
        try(Connection connection = dbUtil.getConnection();) {
            User user = new User();
            String sql = "SELECT * FROM user WHERE username=? AND password=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                return user;
            }




        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> selectAll() {
       /*
            将user表中的所有用户查询出来，每个用于以一个User对象表示，并将这些User对象
            最终存入到一个List集合后将其返回。
            步骤:
            1:创建一个List集合
            2:查询user表中的所有数据
            3:遍历结果集，将每条数据封装到一个User对象中并存入List集合
            4:遍历完毕后，将List集合返回即可
        */
        List<User> userList = new ArrayList();
        try (Connection connection = dbUtil.getConnection();){
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM user";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                userList.add(user);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public int update(User user) {
        List<User> userList = new ArrayList();
        try (Connection connection = dbUtil.getConnection();){
            String sql2= "SELECT username FROM user WHERE username=?";
            PreparedStatement ps2 = connection.prepareStatement(sql2);
            ps2.setString(1,user.getUsername());
            ResultSet rs =ps2.executeQuery();
            if (rs.next()){
                System.out.println("数据库中有该用户");
                String sql = "UPDATE user SET username =?,password =? ,nickname =? ,age=? WHERE username =?";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1, user.getUsername());
                ps.setString(2,user.getPassword());
                ps.setString(3, user.getNickname());
                ps.setInt(4,user.getAge());
                ps.setString(5,user.getUsername());
                int result = ps.executeUpdate();
                return result;
            }else {
                System.out.println("数据库中没有该用户");
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return 0;
    }


}


